package br.ucb.sirad.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.ucb.sirad.beans.TipoDocumento;

public class TipoDocumentoDAO extends DAO<TipoDocumento>{
	
	private EntityManager em;
	
	public TipoDocumentoDAO() {
		super(TipoDocumento.class);
	}

	public List<TipoDocumento> pesquisarPorNomePaginado(String nome,int firstResult, int maxResults){
		EntityManager em = new JPAUtil().getEntityManager();
		em.getTransaction().begin();
		
		Query query = em.createQuery("from "+TipoDocumento.class.getName()+" where nome like :nome and icExclusao is false")
						.setParameter("nome", "%"+nome+"%");
		

		query = query.setFirstResult(firstResult).setMaxResults(maxResults);
				
		List<TipoDocumento> lista = query.getResultList();
		em.getTransaction().commit();
		em.close();

		return lista;
	}
	
	@Override
	public List<TipoDocumento> listaTodos() {
		EntityManager em = new JPAUtil().getEntityManager();
		em.getTransaction().begin();
		
		Query query = em.createQuery("from "+TipoDocumento.class.getName()+" where icExclusao is false");
		
		List<TipoDocumento> lista = query.getResultList();
		em.getTransaction().commit();
		em.close();

		return lista;
	}
	
	@Override
	public int contaTodos() {
		EntityManager em = new JPAUtil().getEntityManager();
		em.getTransaction().begin();
		
		Query query = em.createQuery("select count(*) from "+TipoDocumento.class.getName()+" where icExclusao is false");
		
		long total = (Long) query.getSingleResult();
		em.getTransaction().commit();
		em.close();

		return (int) total;
	}

}
